let http = require('http');
let fs = require('fs');
let path = require('path');

let server = http.createServer();

server.on('request',(req,res) => {
    console.log(req.url);
    let url = req.url;
    res.setHeader('Content-type','text/html;charset=utf-8');
    if(url == '/' || url == '/index') {
        readFun('index.html',res);
    }else if(url == '/register') {
        readFun('/register.html',res);
    }else if(url == '/home') {
        readFun('/home.html',res);
    }else if(url == '/login') {
        readFun('/login.html',res);
    }else if(url == '/index.css') {
        res.setHeader('Content-type','text/css;charset=utf-8');
        readFun('/index.css',res);
    }else {
        readFun('/404.html',res);
    }
    
})

server.listen(8001,()=>{
    console.log('服务器运行在http://localhost:8001上')
});

const readFun = (fileName,response) => {
    fs.readFile(path.join(__dirname,fileName),(err,data) => {
        if(err) return console.log(err);
        response.end(data);
    })
}